//
//  SEGMessageLikeViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/5.
//

#import "SEGMessageLikeViewModel.h"

#import "MessageDataBaseModel.h"
#import "MessageRequestModel.h"

@implementation SEGMessageLikeViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self seg_createCommand];
    }
    return self;
}
- (void)seg_createCommand {
    @weakify(self);
    self.command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString *groupId) {
        @strongify(self);
        self.dataArray = [[MessageDataBaseModel sharedDataBaseModel] getMessagesDescendingWithGroupId:groupId];
        NSMutableString *ids = [NSMutableString string];
        for (int i = 0; i < [self.dataArray count]; i++) {
            MessageModel* msgModel = [self.dataArray objectAtIndex:i];
            [ids appendFormat:@"%@,", msgModel.sendUserId];
        }
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //请求用户信息
            [[MessageRequestModel sharedRequestModel] requestUserMessageWithUsersId:ids finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                self.userInfoDict = [MessageRequestModel sharedRequestModel].userInfoDict;
                [subscriber sendNext:self.dataArray];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    
    }];
}
- (void)seg_pushDetailVCWithModel:(MessageModel *)msgModel successed:(LikePushVCDetailBlock)completionHandler error:(ErrorBlock)errorHandler {
    if (!msgModel.submsg || msgModel.submsg.length == 0) return;
    NSString *type = msgModel.type;
    NSDictionary *subMsgDic = [NSJSONSerialization JSONObjectWithData:[msgModel.submsg dataUsingEncoding:NSUTF8StringEncoding]
                                                              options:NSJSONReadingAllowFragments error:nil];
    // 没有引用原文ID 不做任何响应
    NSString *parentObjId = subMsgDic[@"parentObjId"];
    if (!parentObjId && parentObjId.length <= 0) return;
    
    // PGC详情
    if ([type isEqualToString:kMessageTypePictorial]) {
        NSDictionary *params = @{@"pictorialId":parentObjId};
        if (completionHandler) completionHandler(type, @"", params);
        
    } else {
        // 活动评论详情
        NSString *parentObjType = subMsgDic[@"parentObjType"];
        if ([parentObjType integerValue] == 6) {
            NSDictionary *params = @{@"parentObjId": parentObjId ?: @""};
            if (completionHandler) completionHandler(@"", parentObjType, params);
            return;
        }
        
        // 没有operationType 不做任何响应
        NSString *operationType = subMsgDic[@"operationType"];
        if (!operationType && operationType.length <= 0) {
            if (errorHandler) errorHandler(@"由于用户版本不统一，无法跳转");
            return;
        }
        
        // 邻里圈详情
        NSMutableDictionary *params = [@{
                                 @"objId" : parentObjId,
                                 @"objType" : subMsgDic[@"parentObjType"],
                                 @"operationType" : subMsgDic[@"operationType"]
                                 } mutableCopy];
        if (completionHandler) completionHandler(@"", parentObjType, params);
    }
}

@end
